//
//  SEFollowedHospitalsViewController.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/17.
//

import UIKit

class SEFollowedHospitalsViewController: SEBaseViewController {
    
    private var viewModel = SEFollowedViewModel(role: 1)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.updateData = { [weak self] status in
            self?.tableView.se_empty?.allowShow = true
            self?.endRefreshing()
            switch status {
            case .success:
                self?.tableView.reloadData()
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.loadData(more:)), handle: {
                    SEHUD.showError(message: msg)
                })
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.followedHandler = { [weak self] status in
            switch status {
            case .success:
                self?.tableView.se_header?.beginRefreshing()
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestFollowedHospital), handle: {
                    SEHUD.showError(message: msg)
                })
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }
    
    override func setupUI() {
        super.setupUI()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().priority(.low)
            make.bottom.equalTo(-UIScreen.bottomSafeHeight)
        }
        
        tableView.se_header = SERefreshHeader { [weak self] in self?.loadData(more: false) }
        tableView.se_header?.beginRefreshing()
        tableView.se_footer = SERefreshFooter { [weak self] in self?.loadData(more: true) }
        tableView.se_empty = SEEmptyView { [weak self] in self?.tableView.se_header?.beginRefreshing() }
    }

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .background
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 0.001))
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 110
        tableView.rowHeight = UITableView.automaticDimension
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(SEFollowedHospitalsCell.nibForCell(),
                           forCellReuseIdentifier: SEFollowedHospitalsCell.reuseIdentifier())
        
        return tableView
    }()


    // MARK: - Navi Push

    private func pushHospitalDetail() {
        let vc = SEHospitalDetailViewController(with: viewModel.selectedModel.id ?? "")
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension SEFollowedHospitalsViewController {
    @objc private func loadData(more: Bool) {
        viewModel.getListOfFollowed(loadMore: more)
    }

    @objc private func requestFollowedHospital() {
        viewModel.requestFollowed()
    }
    
    private func endRefreshing() {
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }
}

// MARK: - UITableViewDataSource

extension SEFollowedHospitalsViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEFollowedHospitalsCell.reuseIdentifier()) as! SEFollowedHospitalsCell
        cell.delegate = self
        let vm = viewModel.viewModel(at: indexPath)
        cell.update(with: vm, indexPath: indexPath)
        return cell
    }
}

// MARK: - UITableViewDelegate

extension SEFollowedHospitalsViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 0.001))
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenHeight, height: 1))
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        viewModel.selectedModel = viewModel.followedList[indexPath.section]
        pushHospitalDetail()
    }
}

// MARK: - SEFollowedHospitalsCellDelegate

extension SEFollowedHospitalsViewController: SEFollowedHospitalsCellDelegate {
    func followedHospitalsCellBtnAction(indexPath: IndexPath) {
        viewModel.selectedModel = viewModel.followedList[indexPath.section]
        requestFollowedHospital()
    }
}

